<?php

namespace app\common\model;

use basic\ModelBasic;

class DistributionSeparate extends ModelBasic
{
	use \traits\ModelTrait;

	//关联类型 1服务 2商品 3套餐
	const REL_TYPE_SERVICE = 1;
	const REL_TYPE_GOODS   = 2;
	const REL_TYPE_PACKAGE = 3;

    /**
     * [$reltion_type_mapping description]
     * @var [type]
     */
    public static $reltion_type_mapping = [
        'service'  => 1,
        'goods'    => 2,
        'package'  => 3,
    ];

    public static $reltion_type = [
        1  =>  '服务',
        2  =>  '商品',
        3  =>  '套餐',
    ];
	/**
	 * 保存配置
	 * @Author   FW
	 * @DateTime 2021-09-09
	 * @Email    123763712@qq.com
	 * @param    [type]           $post [description]
	 * @return   [type]                 [description]
	 */
	public static function save_commission ($post)
    {
        $data = [
            'relation_type' => $post['relation_type'],
            'relation_id'   => $post['relation_id'],
            'is_open'       => $post['is_open'] ?? 0,
            'share_poster'  => $post['share_poster'] ?? '',
        ];
        foreach ( $post['default_commission'] as $v ) {
            $data[$v['level_key'] . '_' . 'separate'] = $v['separate'];
            $data[$v['level_key'] . '_' . 'fission'] = $v['fission'];
        }
        return self::saveOne($data);
    }

    /**
     * 获取配置
     * @Author   FW
     * @DateTime 2021-09-09
     * @Email    123763712@qq.com
     * @param    [type]           $relation_type [description]
     * @param    [type]           $relation_id   [description]
     * @return   [type]                          [description]
     */
    public static function get_commission ( $relation_type , $relation_id )
    {
        $default_commission = [
            [
                "level_key"  => "senior",
                "level_name" => lang('Senior Partner'),
                "separate"   => 0,
                "fission"    => 0,
            ],
            [
                "level_key"  => "first",
                "level_name" => lang('First Partner'),
                "separate"   => 0,
                "fission"    => 0,
            ],
            [
                "level_key"  => "second",
                "level_name" => lang('Second Partner'),
                "separate"   => 0,
                "fission"    => 0,
            ],
            [
                "level_key"  => "third",
                "level_name" => lang('Third Partner'),
                "separate"   => 0,
                "fission"    => 0,
            ]
        ];

        $data = self::where(['relation_type' => $relation_type , 'relation_id' => $relation_id])->find();
        if ( !empty($data) ) {
            foreach ( $default_commission as &$v ) {
                $separate = $v['level_key'] . '_' . 'separate';
                $v['separate'] = $data[$separate] ? ($data[$separate]*1) : 0;

                $fission = $v['level_key'] . '_' . 'fission';
                $v['fission'] = $data[$fission] ? ($data[$fission]*1) : 0;
            }
        }
        return $default_commission;
    }

}//Class End
